////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  Filename:    VictoryGuts.cpp                                              //
//  Developer:   Tommy Riddle                                                 //
//  Create date: 09/03/2007                                                   //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  Description: This is the class implementation for the slot gut of the     //
//               Australian Slot machine called Victorty.                     //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//        Copyright (c) 2007 - Realtime Gaming.  All rights reserved.         //
////////////////////////////////////////////////////////////////////////////////

#include "VictoryGuts.h"

////////////////////////////////////////////////////////////////////////////
/// VictoryGuts::m_aLineWinSpinResults
///
/// This array is used to quickly get the SpinResult for the number of
/// icons.  It is indexed by wheel icon and then count.  So to get the
/// SpinResult for 5 Aces, use the following code snippet:
///
///		SpinResult result = m_aLineWinSpinResults[ ICON_ACE ][ 5 ];
///
/// In this example result would be set to EVT_FIVE_ACE.
////////////////////////////////////////////////////////////////////////////
VictoryGuts::SpinResult VictoryGuts::m_aLineWinSpinResults[][6] = 
{
// Number of icons:			0			1					2						3						4						5
/* ICON_NINE */			{	EVT_LOSER,	EVT_LOSER,			EVT_LOSER,				EVT_THREE_NINE,			EVT_FOUR_NINE,			EVT_FIVE_NINE			},
/* ICON_TEN */			{	EVT_LOSER,	EVT_LOSER,			EVT_LOSER,				EVT_THREE_TEN,			EVT_FOUR_TEN,			EVT_FIVE_TEN			},
/* ICON_JACK */			{	EVT_LOSER,	EVT_LOSER,			EVT_LOSER,				EVT_THREE_JACK,			EVT_FOUR_JACK,			EVT_FIVE_JACK			},
/* ICON_QUEEN */		{	EVT_LOSER,	EVT_LOSER,			EVT_LOSER,				EVT_THREE_QUEEN,		EVT_FOUR_QUEEN,			EVT_FIVE_QUEEN			},
/* ICON_KING */			{	EVT_LOSER,	EVT_LOSER,			EVT_LOSER,				EVT_THREE_KING,			EVT_FOUR_KING,			EVT_FIVE_KING			},
/* ICON_ACE */			{	EVT_LOSER,	EVT_LOSER,			EVT_LOSER,				EVT_THREE_ACE,			EVT_FOUR_ACE,			EVT_FIVE_ACE			},
/* ICON_BELL */			{	EVT_LOSER,	EVT_LOSER,			EVT_TWO_BELL,			EVT_THREE_BELL,			EVT_FOUR_BELL,			EVT_FIVE_BELL			},
/* ICON_CANNON */		{	EVT_LOSER,	EVT_LOSER,			EVT_TWO_CANNON,			EVT_THREE_CANNON,		EVT_FOUR_CANNON,		EVT_FIVE_CANNON			},
/* ICON_MARINE */		{	EVT_LOSER,	EVT_LOSER,			EVT_TWO_MARINE,			EVT_THREE_MARINE,		EVT_FOUR_MARINE,		EVT_FIVE_MARINE			},
/* ICON_CAPTAIN */		{	EVT_LOSER,	EVT_ONE_CAPTAIN,	EVT_TWO_CAPTAIN,		EVT_THREE_CAPTAIN,		EVT_FOUR_CAPTAIN,		EVT_FIVE_CAPTAIN		},
/* ICON_FLAG */			{	EVT_LOSER,	EVT_LOSER,			EVT_LOSER,				EVT_LOSER,				EVT_LOSER,				EVT_LOSER				},
/* ICON_SHIP */			{	EVT_LOSER,	EVT_LOSER,			EVT_LOSER,				EVT_LOSER,				EVT_LOSER,				EVT_LOSER				}
};

////////////////////////////////////////////////////////////////////////////
/// VictoryGuts::m_aScatterWinSpinResults
///
/// This array is used to quickly get the SpinResult for the number of
/// icons.  It is indexed by icon count only, since Flags are the only
/// scatter win.  So to get the SpinResult for 5 Flags, use the
/// following code snippet:
///
///		SpinResult result = m_aScatterWinSpinResults[ 5 ];
///
/// In this example result would be set to EVT_FIVE_FLAG.
////////////////////////////////////////////////////////////////////////////
VictoryGuts::SpinResult VictoryGuts::m_aScatterWinSpinResults[6] = 
{
	// ICON_FLAG        Number of Icons
	EVT_LOSER,			// 0
	EVT_LOSER,			// 1
	EVT_LOSER,			// 2
	EVT_THREE_FLAG,		// 3
	EVT_FOUR_FLAG,		// 4
	EVT_FIVE_FLAG		// 5
};

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::VictoryGuts()   
///
/// This is the default constructor for the class.
////////////////////////////////////////////////////////////////////////////
VictoryGuts::VictoryGuts()
{
	// If this doesn't compile it means that someone probably added
	// another icon type but did not update the
	// m_aLineWinSpinResults array. 
	C_ASSERT( countof( m_aLineWinSpinResults ) == NUM_VALID_ICONS );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::~VictoryGuts()
///
/// Destructor for VictoryGuts. 
////////////////////////////////////////////////////////////////////////////
VictoryGuts::~VictoryGuts()
{
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::SpinResult VictoryGuts::getLineWinResultWithPossibleSubstitution( uint8* stops, uint8 testIcon, bool& bSubstitution ) const
///
/// @param   stops			The array of stop positions for the line.
/// @param   testIcon		The icon we are looking for, typically the first icon on the line.
/// @param   bSubstitution	An output parameter that is set if a subsitution is used.
///
/// @return	 SpinResult		A payout from the SpinResult enumeration.
///
/// This helper method is used to calculate the line win payout with a possible substitution.
////////////////////////////////////////////////////////////////////////////
VictoryGuts::SpinResult VictoryGuts::getLineWinResultWithPossibleSubstitution(
	uint8* stops, uint8 testIcon, bool& bSubstitution ) const
{
	// Initialize the out parameter
	bSubstitution = false;

	// Loop through the remaining reels until the streak is over
	for ( int i = 1; i < getReelCount(); i++ )
    {
		// See what this icon is.
		uint8 currentIcon = getIcon( getGameModeId(), i, stops[i] )->iconid;

		// If the current icon is a Ship...
		if ( currentIcon == ICON_SHIP )
		{
			// then use it as a substitution symbol and continue on.
			bSubstitution = true;
			continue;
		}
		// Otherwise if the test icon is a Ship...
		else if ( testIcon == ICON_SHIP )
		{
			// then we still have a substitution.  In this case the line
			// started out with a Ship, which should never happen in this game.
			ASSERT( false );
			break;
		}

		// If the current icon is not the test icon...
        if ( currentIcon != testIcon )
        {
			// then the streak is over so break out of the loop
			break;
        }
    }

	// This should never happen in this game because there aren't 
	// any ships on the first or last reel.
	ASSERT( testIcon != ICON_SHIP )

	// Use the array to look up the spin result for i testIcons.
	return lookupLineWinSpinResult( testIcon, i );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::classifyStops()   
///
/// @param   stops      The array of stop positions for the line.
/// @param   bet        The value of the line bet.
/// @param   line       The index of the line.
///
/// @return	 uint8      A payout from the SpinResult enumeration as a uint8.    
///
/// This helper method is used to calculate the payout on a line.
////////////////////////////////////////////////////////////////////////////
void VictoryGuts::classifyStops( SpinLine* spinline, uint8* stops, uint8 bet, uint8 line,
	uint8& rtnresult, uint8& multiplier )
{
	// See what the payout is if we use any Pilots we find as substitution symbols.
	// Start with the icon on the first reel.
	uint8 testIcon = getIcon( getGameModeId(), 0, stops[0] )->iconid;

	// Declare a local variable to track whether or not we found a substitution.
	bool bSubstitution = false;

	// See what the spin result is if we used any Pilots as substitution symbols.
	rtnresult =	getLineWinResultWithPossibleSubstitution( stops, testIcon, bSubstitution );

	// If we did actually have a substitution and a winning spin...
	if ( bSubstitution && rtnresult != EVT_LOSER )
	{
		// then see what game mode we are in
		if ( getGameModeId() == MODE_NORMAL )
		{
			// then just add a the normal note.
			spinline->addNote( NOTE_SUBSTITUTION );
		}
		// Otherwise...
		else
		{
			// we must be in the free game mode.
			ASSERT( getGameModeId() == MODE_FREE_SPIN );

			// then add a note.
			spinline->addNote( NOTE_FREE_SPIN_SUBSTITUTION );
			multiplier = MULTIPLIER_FREE_SPIN_SUBSTITUTION;
		}
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn		VictoryGuts::tryForJackpot()
///
///	This function looks to see if we hit the jackpot.
////////////////////////////////////////////////////////////////////////////
void VictoryGuts::tryForJackpot( SlotSpin* spin )
{
	// If we are in the first paid spin (NOT a bonus spin)...
	if ( getGameModeId() == MODE_NORMAL ) 
	{
		// then try for the Random Progressive Jackpot
		// If we hit the jackpot...
		if ( getRandomProgressiveJackpotResult( getNumLinesBet() ) || 
             m_bForceRandomJackpot == true )
		{
			// then add a jackpot scatter to the spin.
			spin->addScatter( 0,
				getPayout( EVT_RANDOM_PROGRESSIVE_WINNER )->payoutAmount, 1,
				true, EVT_RANDOM_PROGRESSIVE_WINNER, 0 );
		}
    }
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::getScatterWinResult()   
///
/// @param   spin						The current spin object.
/// @param   nNumScatterIcons			The number of scatter icons that we find.
/// @param   nScatterPositions			The bit mask for the scatters positions that we find.
///
/// @return	 SpinResult			A payout from the SpinResult enumeration.
///
/// This helper method is used to evaluate the spin for scatters.
////////////////////////////////////////////////////////////////////////////
VictoryGuts::SpinResult VictoryGuts::getScatterWinResult( SlotSpin* spin, int32& nNumScatterIcons, int32& nScatterPositions, bool& bSubstitution ) const
{
	// Initialize the scatter positions.
	nScatterPositions = 0;

	// We don't have a substitution at this point.
	bSubstitution = false;

	// Loop through each reel.
  	for( int reel = 0; reel < getReelCount(); reel++ )
	{
		// The position bit for the current icon in case it needs to be stored in a mask.
		uint8 position = reel;

		// Loop through each row in the wheel house.
		for( int row = 0; row < 3; row++ )
		{
			// Get the current stop.
			uint8 stop = relativeWheelPosition( getGameModeId(), reel,
				spin->getStops()[ reel ], row );

			// Get the icon for the current stop.
			uint8 currentIcon = getIcon( getGameModeId(), reel, stop )->iconid;
			
			// If the icon is the one we are looking for...
			if ( currentIcon == ICON_FLAG )
			{
				// then increase the icon count
				nNumScatterIcons++;

				// Set the bit in the position mask for this stop.
				nScatterPositions |= 1 << position;
			}
			// Otherwise if the icon is a substitution...
			else if ( currentIcon == ICON_SHIP )
			{
				// then increase the icon count
				nNumScatterIcons++;

				// Set the bit in the position mask for this stop.
				nScatterPositions |= 1 << position;

				// Set the substitution flag.
				bSubstitution = true;
			}

			// Move to the next bit in the position mask.
            position += 5;
		}
	}

	// Use the array to look up the spin result for nNumScatterIcons.
	return lookupScatterWinSpinResult( nNumScatterIcons );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::addScatterWin()
///
/// This function determines if the current spin wins a scatter payment and
/// adds the scatter.
////////////////////////////////////////////////////////////////////////////
void VictoryGuts::addScatterWin( SlotSpin* spin, SpinResult srScatterResult, int32 nScatterPositions, bool bSubstitution )
{
	// If we got the scatter win...
	if ( srScatterResult != EVT_LOSER )
	{
		// then initialize our multiplier for scatters.
		int32 multiplier = 1;

		// If we are in free spin mode...
		if ( getGameModeId() == MODE_FREE_SPIN )
		{
			// then use the free spin scatter multiplier.
			multiplier = MULTIPLIER_FREE_SPIN;

			// If we also had a substitution...
			if ( bSubstitution )
			{
				// then also award the substitution multiplier.
				multiplier *= MULTIPLIER_FREE_SPIN_SUBSTITUTION;
			}
		}

		// Get the base payout for the scatter.
		int32 nPayout = getPayout( srScatterResult )->payoutAmount;
		
		// Scatters payout based on the total bet, so factor in the number of bet lines.
		nPayout *= spin->getSpinLineCount();

		// Add the payout (with multiplier) to the total win for this spin.
		increaseTotalWin( nPayout * multiplier );
    
		// Add a scatter to the spin.
		SlotScatter* pScatter = spin->addScatter( nScatterPositions,
			nPayout * getBetPerLine(), multiplier, false, srScatterResult, 0 );

		// If we are in free spin mode...
		if ( getGameModeId() == MODE_FREE_SPIN )
		{
			// then add the free spin note.
			pScatter->addNote( NOTE_FREE_SPIN );

			// If we also had a substitution...
			if ( bSubstitution )
			{
				// then also add the free spin substitution note.
				pScatter->addNote( NOTE_FREE_SPIN_SUBSTITUTION );
			}
		}
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::addFeatureTrigger()
///
/// This function determines if the current spin triggered the free game
/// feature and adds the appropriate scatter.
////////////////////////////////////////////////////////////////////////////
void VictoryGuts::addFeatureTrigger( SlotSpin* spin, int32& nNumScatterIcons, int32 nFeatureScatterPositions )
{
	// We need a place to keep up with how many free spins to award.
	int32 nFreeSpins = 0;

	// Also keep up with what type of feature trigger it is.
	SpinResult srFeatureResult = EVT_LOSER;

	// Figure out how many free spins they get.
	if ( nNumScatterIcons == 1 )
	{
		srFeatureResult = EVT_FEATURE_NEAR_MISS;
	}
	else if ( nNumScatterIcons == 2 )
	{
		srFeatureResult = EVT_FEATURE_NEAR_MISS;
	}
	else if ( nNumScatterIcons == 3 )
	{
		nFreeSpins = 12;
		srFeatureResult = EVT_FREE_GAME_FEATURE;
	}
	else if ( nNumScatterIcons == 4 )
	{
		nFreeSpins = 20;
		srFeatureResult = EVT_FREE_GAME_FEATURE;
	}
	else if ( nNumScatterIcons == 5 )
	{
		nFreeSpins = 50;
		srFeatureResult = EVT_FREE_GAME_FEATURE;
	}

	// If they get free spins...
	if ( nFreeSpins )
	{
		// then change the game mode.
		setGameModeId( MODE_FREE_SPIN );

		// Add the free spins.
		incrementFreeSpins( nFreeSpins );
	}

	// If we had a trigger or a near miss...
	if ( srFeatureResult != EVT_LOSER )
	{
		// then add a scatter to the spin for the feature trigger.
		spin->addScatter( nFeatureScatterPositions, 0, 0, false,
			srFeatureResult, nFreeSpins );
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::classifyScatters( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This is an override of the framework method.  
/// It determines if the current spin merits free spins or scatter payments.
////////////////////////////////////////////////////////////////////////////
void VictoryGuts::classifyScatters( SlotSpin* spin )
{	
	// Try for the Random Progressive Jackpot
	tryForJackpot( spin );

	// Declare some local variables to track the scatter positions.
	int32 nScatterPositions = 0;
	int32 nNumScatterIcons = 0;

	// Declare a local variable to track whether or not we found a substitution.
	bool bSubstitution = false;

	// See if there is a scatter win.
	SpinResult srResult = getScatterWinResult( spin, nNumScatterIcons, nScatterPositions, bSubstitution );

	// Add the scatter, if any.
	addScatterWin( spin, srResult, nScatterPositions, bSubstitution );

	// Add the scatters for the free game feature or near miss.
	addFeatureTrigger( spin, nNumScatterIcons, nScatterPositions );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::isValidGuts()   
///
/// @return	 bool8      True if gut is valid.    
///
/// This helper method is used to see if the current gut ID is valid.
////////////////////////////////////////////////////////////////////////////
bool8 VictoryGuts::isValidGuts()
{
	// Make sure that the m_aLineWinSpinResults array has an entry for each reel,
	// and an additional entry in case there are 0 icons on the line.
	// Note:  we can't do this in the constructor because we have to call
	// getReelCount.
	ASSERT( countof( m_aLineWinSpinResults[0] ) == getReelCount( MODE_NORMAL ) + 1 );

	// Make sure that the m_aScatterWinSpinResults array has an entry for each reel,
	// and an additional entry in case there are 0 icons on the line.
	ASSERT( countof( m_aScatterWinSpinResults ) == getReelCount( MODE_NORMAL ) + 1 );

	// Now make sure that all game modes have the same number of reels.
	ASSERT( getReelCount( MODE_NORMAL ) == getReelCount( MODE_FREE_SPIN ) );

	// Finally just check to see if the gut is within the right range.
    return ( getGutsId() >= 2400 && getGutsId() < 2500 );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::getGameModeMultiplierAndNoteId( uint8& nGameModeMultiplier, uint8& nNoteId )
///
/// @param   nGameModeMultiplier	The multiplier for the game mode.
/// @param   nNoteId				The note ID for the game mode.
///
/// This helper method is used to get the game mode multiplier and note ID.
////////////////////////////////////////////////////////////////////////////
void VictoryGuts::getGameModeMultiplierAndNoteId( uint8& nGameModeMultiplier,
	uint8& nNoteId )
{
	// If we are in free spin mode...
    if ( getGameModeId() != MODE_NORMAL )
    {
		// then return the free spin multiplier...
        nGameModeMultiplier = MULTIPLIER_FREE_SPIN;

		// and the free spin note.
        nNoteId = NOTE_FREE_SPIN;
    }
	// Otherwise...
    else
    {
		// just defer to the base class.
        SlotGuts::getGameModeMultiplierAndNoteId( nGameModeMultiplier, nNoteId );
    }
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 VictoryGuts::Debug_SetLoserStops( uint8* pStops )
///
/// @param   pStops		The stops to set.
///
/// Base class override, returns a losing spin triggered by the debug tool
////////////////////////////////////////////////////////////////////////////
void VictoryGuts::Debug_SetLoserStops( uint8* pStops )
{
	pStops[0]=3;
	pStops[1]=0;
	pStops[2]=0;
	pStops[3]=0;
	pStops[4]=0;
}
